package com.dissertation.action;

import com.dissertation.pojo.Favorite;
import com.dissertation.pojo.Message;
import com.dissertation.respository.CollectionRepository;
import com.dissertation.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("collection")
public class CollectionController {


    @Autowired
    ICollectionService collectionService;

    @Autowired
    CollectionRepository collectionRepository;


    @PostMapping("addCollection")
    public Message addCollection(@RequestBody Map<String,Object> params){
        return collectionService.addCollection(params);
    }

    /**
     * @param params
     * @return
     */
    @DeleteMapping("removeCollection")
    public Message removeCollection(@RequestBody Map<String,Object> params){
        return collectionService.removeCollection(params);
    }

    @DeleteMapping("column")
    public Message removeColumn(@RequestParam String column){
        return collectionService.removeColumn(column);
    }


    @PostMapping("addColumn")
    public Message addColumn(@RequestParam String column){
        return collectionService.addColumn(column);
    }

    @GetMapping("columns")
    public Message getColumns(){
        return Message.createSuc(collectionService.getColumns());
    }

    @GetMapping("favorites")
    public Message getFavorites(@RequestParam String column){
        return Message.createSuc(collectionService.getFavorites(column));
    }

    @GetMapping("search")
    public Message search(@RequestParam String keyword){
        return Message.createSuc(collectionService.searchBykeyword(keyword));
    }

    @GetMapping("favoritesTemplate")
    public Message getTemplate(HttpServletResponse response){
        String[] ids = null;
        return Message.createSuc(collectionService.importFavorites(ids,"收藏夹模板.xlsx",response));
    }
    @GetMapping("favoritesAll")
    public Message getFavoritesAll(@RequestParam String column,HttpServletResponse response){
        List<Favorite> favoriteList = collectionService.getFavorites(column);
        List<String> idList = new ArrayList<>();
        favoriteList.forEach(e->idList.add(e.getAssetsID()));
        String[] ids = new String[idList.size()];
        idList.toArray(ids);
        return Message.createSuc(collectionService.importFavorites(ids,column+"栏目中的收藏内容.xlsx",response));
    }

    @GetMapping("favoritesSelected")
    public Message getFavoritesSelected(@RequestParam String[] ids,HttpServletResponse response){
        return Message.createSuc(collectionService.importFavorites(ids,"所选择的收藏内容.xlsx",response));
    }


}
